﻿/***********************************************************************
 * <copyright file="SiteRepository.cs" company="B/S JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   Hoang Bich Son 
 * Email:    sonhb@newman.vn
 * Website:
 * Create Date: Friday, March 14, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using DxMvcApplication.Repositories.Core;
using DxMvcApplication.Repositories.Core.Security;
using DxMvcApplication.SecurityServiceReference;
using NmResources;

namespace DxMvcApplication.Repositories.Implementations.Security
{
    /// <summary>
    /// SiteRepository class
    /// </summary>
    public class SiteRepository : SecurityRepositoryBase, ISiteRepository
    {
        /// <summary>
        /// Gets the list.
        /// </summary>
        /// <param name="userId">The user identifier.</param>
        /// <param name="parentId">The parent identifier.</param>
        /// <returns></returns>
        public List<SiteDto> GetList(int userId, int parentId)
        {
            var request = new SiteRequest().Prepare();
            request.LoadOptions = new[] { "SitesByUser" };
            request.Criteria = new SiteCriteria { UserId = userId, ParentId = parentId };
            var response = SecurityServiceClient.GetSites(request);
            Correlate(request, response);
            return response.Sites == null ? null : response.Sites.ToList();
        }

        /// <summary>
        /// Gets the list by parent identifier.
        /// </summary>
        /// <param name="parentId">The parent identifier.</param>
        /// <returns></returns>
        public List<SiteDto> GetListByParentId(int parentId)
        {
            var request = new SiteRequest().Prepare();
            request.LoadOptions = new[] { "SitesByParentId" };
            request.Criteria = new SiteCriteria { ParentId = parentId };
            var response = SecurityServiceClient.GetSites(request);
            Correlate(request, response);
            return response.Sites == null ? null : response.Sites.ToList();
        }

        /// <summary>
        /// Gets the search paged.
        /// </summary>
        /// <param name="keyword">The keyword.</param>
        /// <param name="status">if set to <c>true</c> [status].</param>
        /// <param name="sort">The sort.</param>
        /// <param name="order">The order.</param>
        /// <param name="totalPage">The total page.</param>
        /// <param name="currentPage">The current page.</param>
        /// <returns></returns>
        public List<SiteDto> GetAdvancedSearch(string keyword, bool? status, string sort, string order, ref int totalPage, int currentPage = 1)
        {
            var request = new SiteRequest().Prepare();
            request.LoadOptions = new[] { "Search" };
            request.CurrentPage = currentPage;
            request.TotalPage = totalPage;
            request.Criteria = new SiteCriteria { Keyword = keyword, Status = status, SortExpression = sort, OrderExpression = order };
            var response = SecurityServiceClient.GetSites(request);
            Correlate(request, response);
            totalPage = response.TotalPage;
            return response.Sites == null ? null : response.Sites.ToList();
        }

        public int GetPageSize()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets the list.
        /// </summary>
        /// <param name="criterion">The criterion.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public List<SiteDto> GetList(Criterion criterion = null)
        {
            var request = new SiteRequest().Prepare();
            request.LoadOptions = new[] { "Sites" };
            var response = SecurityServiceClient.GetSites(request);
            Correlate(request, response);
            return response.Sites == null ? null : response.Sites.ToList();
        }

        /// <summary>
        /// Gets the paged.
        /// </summary>
        /// <param name="totalPage">The total page.</param>
        /// <param name="currentPage">The current page.</param>
        /// <returns></returns>
        public List<SiteDto> GetPaged(ref int totalPage, int currentPage = 1)
        {
            var request = new SiteRequest().Prepare();
            request.LoadOptions = new[] { "Sites" };
            request.OptionTypes = OptionType.IsPaging;
            request.CurrentPage = currentPage;
            request.TotalPage = totalPage;
            var response = SecurityServiceClient.GetSites(request);
            Correlate(request, response);
            totalPage = response.TotalPage;
            return response.Sites == null ? null : response.Sites.ToList();
        }

        /// <summary>
        /// Gets the specified identifier.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        public SiteDto Get(int id)
        {
            var request = new SiteRequest().Prepare();
            request.LoadOptions = new[] { "Site" };
            request.Criteria = new SiteCriteria { Id = id };
            var response = SecurityServiceClient.GetSites(request);
            Correlate(request, response);
            return response.Site;
        }

        /// <summary>
        /// Inserts the specified t.
        /// </summary>
        /// <param name="t">The t.</param>
        /// <returns></returns>
        public string Insert(SiteDto t)
        {
            var request = new SiteRequest().Prepare();
            request.Action = "Insert";
            request.Site = t;
            var response = SecurityServiceClient.SetSites(request);
            Correlate(request, response);
            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
            return response.MessageCode.MapperResource();
        }

        /// <summary>
        /// Updates the specified t.
        /// </summary>
        /// <param name="t">The t.</param>
        /// <returns></returns>
        public string Update(SiteDto t)
        {
            var request = new SiteRequest().Prepare();
            request.Action = "Update";
            request.Site = t;
            var response = SecurityServiceClient.SetSites(request);
            Correlate(request, response);
            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
            return response.MessageCode.MapperResource();
        }

        /// <summary>
        /// Deletes the specified identifier.
        /// </summary>
        /// <param name="idList">The identifier list.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public string Delete(IList<int> idList)
        {
            var request = new SiteRequest().Prepare();
            request.Action = "Delete";
            request.Criteria = new SiteCriteria { IdList = string.Join(",", idList) };
            var response = SecurityServiceClient.SetSites(request);
            Correlate(request, response);
            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
            return response.MessageCode.MapperResource();
        }
    }
}